<?php
/**
 * Created by PhpStorm.
 * User: alei
 * Date: 11/5/2017 AD
 * Time: 17:09
 */
namespace App\Models\Mysql;

//use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\BaseDBMethod;

class pre_forum_post
{
	use BaseDBMethod;
	//数据库连接配置项，见config/database.php
	const DATABASE_CONF = 'lqyw_mysql';
	//pre_ct_ui 首页轮播图所在表
	const TABLE_NAME = 'pre_forum_post';

	//数据库连接配置项，见config/database.php
	//如果需要用ORM，需要设置此项
//	protected $connection = self::DATABASE_CONF;
	//表名，如果需要用ORM，需要设置此项
//	protected $table = self::TABLE_NAME;

	public static function get_pid_forum_post(&$param)
	{
		//tid: 译文对应论坛主题帖的tid
		return self::getConnObj()->select(
			"select pid from pre_forum_post where tid=:tid AND first=1",
			[
				'tid' => $param['tid']
			]
		);
	}

	public static function first_pid_forum_post($param)
	{
		$all_tid = self::get_pid_forum_post($param);
		if($all_tid && isset($all_tid[0])) {
			return $all_tid[0]->pid;
		}
		return '';
	}

	public static function fetch_first($param)
	{
		$result = self::getConnObj()->select(
			"select ".$param['field']." from pre_forum_post where tid=:tid AND first=1",
			[
				'tid' => $param['tid']
			]
		);
		if($result) return $result[0];
		else return null;
	}

	#获取某个任务的翻译列表
    public static function get_list($param)
    {

        return self::getConnObj()->select(
            "SELECT b.pid, b.tid,b.`subject`,b.message,b.author,b.dateline FROM pre_forum_post b WHERE b.tid =:tid and pid <>:pid order by b.dateline desc limit {$param['start']},{$param['end']}  ",
            [
                'pid' => $param['pid'],
                'tid' => $param['tid'],
            ]
        );

    }

    #取出某个任务的所有译文列表
	public static function getTranslationListOfTask($srctid){
		return self::getConnObj()->select(
			"SELECT ct_tran.tid, author, authorid, fp.dateline, subject, message ".
			"from pre_ct_task ct_task, pre_ct_translatedtext ct_tran, pre_forum_post fp ".
			"where ct_task.taskid=ct_tran.taskid and ct_tran.tid=fp.tid and fp.first=1 and ct_task.srctid=".$srctid
		);
	}
   
    #获取单条翻译数据
    public static  function  get_info($param)
    {
        return self::getConnObj()->select(
            "SELECT b.pid,b.tid,b.`subject`,b.message,b.author,b.authorid,b.dateline FROM pre_forum_post b WHERE pid=:pid limit 1",
            [
                'pid' => $param['pid'],
            ]
        );
    }

    #统计评论某个翻译的条数
    public static function count_comment($param)
    {
        return self::getConnObj()->select(
            "SELECT count(*) count FROM  pre_forum_post   WHERE tid=:tid and pid <> :pid ",
            [
                'tid' => $param['tid'],
                'pid' => $param['pid'],
            ]
        );
    }

    #获取某个翻译下的评论列表
    public static function comment_list($param)
    {
        return self::getConnObj()->select(
            "SELECT b.tid,b.`subject`,b.message,b.author,b.authorid, b.dateline FROM  pre_forum_post b 
            WHERE b.tid =({$param['tid']}) and pid <> {$param['pid']} order by b.dateline desc limit {$param['start']},{$param['end']}  "

        );

    }
    #获取某个翻译下的评论列表总数
    public static function comment_count($param)
    {
        return self::getConnObj()->select(
            "SELECT count(*) count FROM  pre_forum_post   WHERE tid=:tid and pid <> {$param['pid']} ",
            [
                'tid' => $param['tid'],
            ]
        );
    }

	public static function getMessageOfBoardByForumID($param)
	{
		if( !$param || !isset($param['fid']) ) return [];
		return self::getConnObj(TRUE)->select(
			"SELECT ft.subject,message AS body,ft.tid,fp.pid from `pre_forum_thread` AS ft, ".
			"`pre_forum_post` AS fp where ft.tid=fp.tid AND fp.first=1 AND ft.fid=:fid ".
			"ORDER BY ft.dateline DESC LIMIT ".($param['pageNO']-1)*$param['pageSize'].",".$param['pageSize'], [
				'fid' => $param['fid']
			]
		);
/*		return self::getConnObj(TRUE)->select(
			"SELECT ft.subject,message AS body,ft.tid,fp.pid from `pre_forum_thread` AS ft, ".
			"`pre_forum_post` AS fp where ft.tid=fp.tid AND fp.first=1 AND ft.fid=:fid ".
			"ORDER BY ft.dateline DESC ".self::limit(($param['pageNO']-1)*$param['pageSize'], $param['pageSize']), [
				'fid' => $param['fid']
			]
		);*/

	}

    #插入回复
    public static function insertReply($param){
		$r=db()->insert('pre_forum_post',array(
			'fid'=>$param['fid'], 'tid'=>$param['tid'], 'pid'=>$param['pid'], 'message' => $param['content'],
			'author' => $param['author'],'authorid' => $param['authorid'],
			'first' => '0','subject' => '','dateline' => $param['dateline'],'port' => 0,'invisible' => 0,'anonymous' => 0,'usesig' => 0,'htmlon' => 0,'bbcodeoff' => -1,'smileyoff' => -1,'parseurloff' => 0,'attachment' => '0','status' => 0
		));
		return $r;
	
	}

    public static function getMaxPositionByTid($tid){
        $r=self::getConnObj()->select("select max(position) maxposition from pre_forum_post where tid=".$tid);
		if (empty($r)){
			return -1;
		}
		else{
			return $r[0]->maxposition;
		}
	}
	
}